package net.lab1024.sa.admin.module.business.yct.yctTeeth.manager;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import net.lab1024.sa.admin.module.business.yct.yctTeeth.domain.entity.DialogueEntity;
import net.lab1024.sa.admin.module.business.yct.yctTeeth.dao.DialogueDao;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.lab1024.sa.admin.module.business.yct.yctTeeth.domain.vo.SendTemplateMessageItemVo;
import net.lab1024.sa.admin.module.business.yct.yctTeeth.domain.vo.TemplateMessageVo;
import net.lab1024.sa.admin.module.business.yct.yctTeeth.service.YctTeethHospitalService;
import net.lab1024.sa.base.common.util.RedisUtil;
import net.lab1024.sa.base.common.util.RestTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * 聊天信息  Manager
 *
 * @Author zhangben
 * @Date 2024-12-20 11:07:08
 * @Copyright 20241218
 */
@Service
public class DialogueManager extends ServiceImpl<DialogueDao, DialogueEntity> {
    @Autowired
    private YctTeethHospitalService yctTeethHospitalService;
    @Autowired
    private RestTemplateUtil restTemplateUtil;
    @Autowired
    private RedisUtil redisUtil;


    public  Boolean sendMiniSubscribeMessage(TemplateMessageVo templateMessage) {
        String accessToken = yctTeethHospitalService.getMiniAccessToken();
        String url = StrUtil.format("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={}", accessToken);
        JSONObject messAge = JSONObject.parseObject(JSONObject.toJSONString(templateMessage));
        String result = restTemplateUtil.postJsonData(url, messAge);
        JSONObject data = JSONObject.parseObject(result);
        if (ObjectUtil.isNull(data)) {
//            throw new CrmebException("微信平台接口异常，没任何数据返回！");
        }
        if (data.containsKey("errcode") && !"0".equals(data.getString("errcode"))) {
            if ("40001".equals(data.getString("errcode"))) {
//                wxExceptionDispose(data, "微信小程序发送订阅消息异常");
                redisUtil.delete("wechat_mini_accessToken");
                accessToken = yctTeethHospitalService.getMiniAccessToken();
                url = StrUtil.format("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={}", accessToken);
                result = restTemplateUtil.postJsonData(url, messAge);
                JSONObject data2 = JSONObject.parseObject(result);
                if (data2.containsKey("errcode") && !"0".equals(data2.getString("errcode"))) {
                    if (data2.containsKey("errmsg")) {
                        System.out.println("1"+data2);
//                        wxExceptionDispose(data2, "微信小程序发送订阅消息重试异常");
//                        throw new CrmebException("微信接口调用失败：" + data2.getString("errcode") + data2.getString("errmsg"));
                    }
                } else {
                    return Boolean.TRUE;
                }
            }
            if (data.containsKey("errmsg")) {
                System.out.println("1"+data);
//                wxExceptionDispose(data, "微信小程序发送订阅消息异常");
//                throw new CrmebException("微信接口调用失败：" + data.getString("errcode") + data.getString("errmsg"));
            }
        }
        return Boolean.TRUE;
    }

    public static void main(String[] args) {
        HashMap<String, String> temMap =  new HashMap<>();
        temMap.put("thing1","消息测试");
        temMap.put("time2","2013-01-12");
        temMap.put("thing3","内容");

        TemplateMessageVo programTemplateMessageVo = new TemplateMessageVo();
        programTemplateMessageVo.setTemplate_id("Gji6K9T0nqary9VnGAOngq1OYhJznxzbx1rfZYbWCX4");

        //组装关键字数据
        HashMap<String, SendTemplateMessageItemVo> hashMap = new HashMap<>();
        temMap.forEach((key, value) -> hashMap.put(key, new SendTemplateMessageItemVo(value)));
        programTemplateMessageVo.setUrl("subpackageA/pages/servicer/servicer");
        programTemplateMessageVo.setData(hashMap);
        programTemplateMessageVo.setTouser("o0ZIW7aRPNL9DGns4O-yEm1GqclU");

//       sendMiniSubscribeMessage(programTemplateMessageVo);
    }

}
